package com.ys.nms.module.business.controller.admin.sysLog;

import com.baomidou.dynamic.datasource.annotation.Slave;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.sysLog.vo.*;
import com.ys.nms.module.business.dal.dataobject.sysLog.SysLogDO;
import com.ys.nms.module.business.service.sysLog.SysLogService;

@Tag(name = "管理后台 - 系统日志")
@RestController
@RequestMapping("/business/sys-log")
@Validated
public class SysLogController {

    @Resource
    private SysLogService sysLogService;

    @PostMapping("/create")
    @Operation(summary = "创建系统日志")
    public CommonResult<Long> createSysLog(@Valid @RequestBody SysLogSaveReqVO createReqVO) {
        return success(sysLogService.createSysLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统日志")
    public CommonResult<Boolean> updateSysLog(@Valid @RequestBody SysLogSaveReqVO updateReqVO) {
        sysLogService.updateSysLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统日志")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSysLog(@RequestParam("id") Long id) {
        sysLogService.deleteSysLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除系统日志")
    public CommonResult<Boolean> deleteSysLogList(@RequestParam("ids") List<Long> ids) {
        sysLogService.deleteSysLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SysLogRespVO> getSysLog(@RequestParam("id") Long id) {
        SysLogDO sysLog = sysLogService.getSysLog(id);
        return success(BeanUtils.toBean(sysLog, SysLogRespVO.class));
    }

    @Slave
    @GetMapping("/page")
    @Operation(summary = "获得系统日志分页")
    public CommonResult<PageResult<SysLogRespVO>> getSysLogPage(@Valid SysLogPageReqVO pageReqVO) {
        PageResult<SysLogDO> pageResult = sysLogService.getSysLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统日志 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSysLogExcel(@Valid SysLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysLogDO> list = sysLogService.getSysLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统日志.xls", "数据", SysLogRespVO.class,
                        BeanUtils.toBean(list, SysLogRespVO.class));
    }

}